package com.bailugansan.efficiencyboard.service.impl;

import com.bailugansan.efficiencyboard.dao.TaskDao;
import com.bailugansan.efficiencyboard.entity.task.AddTaskEntity;
import com.bailugansan.efficiencyboard.entity.task.DeleteTaskEntity;
import com.bailugansan.efficiencyboard.entity.task.SwapTaskEntity;
import com.bailugansan.efficiencyboard.entity.task.UpdateTaskEntity;
import com.bailugansan.efficiencyboard.pojo.Board;
import com.bailugansan.efficiencyboard.service.TaskService;
import com.bailugansan.efficiencyboard.utils.DateUtils;
import com.bailugansan.efficiencyboard.utils.UnixTimeUtils;
import com.bailugansan.efficiencyboard.vo.BoardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author BailuGansan
 */

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskDao taskDao;

    @Override
    public boolean addTask(AddTaskEntity entity) {
        entity.setDrawerStartYear(UnixTimeUtils.TimeStamp2Date(String.valueOf(entity.getDrawerStartTime() / 1000), "yyyy-MM-dd"));
        entity.setDrawerDeadLineYear(UnixTimeUtils.TimeStamp2Date(String.valueOf(entity.getDrawerDeadLineTime() / 1000), "yyyy-MM-dd"));
        int order = getOrder(entity.getDrawerPriority());
        if (order == -2){
            return false;
        }

        boolean result = taskDao.addTask(entity.getPdd(),
                entity.getDrawerTitle(),
                entity.getDrawerPriority(),
                entity.getDrawerDeadLineYear(),
                entity.getDrawerDeadLineTime(),
                entity.getDrawerTaskDescript(),
                entity.getDrawerStartYear(),
                entity.getDrawerStartTime(),
                order);

        return result;
    }

    @Override
    public boolean updateTask(UpdateTaskEntity entity) {

        int order = getOrder(entity.getDrawerPriority());
        if (order == -2){
            return false;
        }

        boolean result = taskDao.updateTask(entity.getPdd(),
                entity.getTaskId(),
                entity.getDrawerTitle(),
                entity.getDrawerPriority(),
                entity.getDrawerDeadLineYear(),
                entity.getDrawerDeadLineTime(),
                entity.getDrawerTaskDescript(),
                entity.getDrawerStartYear(),
                entity.getDrawerStartTime(),
                order);

        return result;
    }

    @Override
    public boolean deleteTask(DeleteTaskEntity entity) {

        boolean result = taskDao.deleteTask(entity.getPdd(), entity.getTaskId());

        return result;
    }

    @Override
    public boolean swapTask(SwapTaskEntity entity) {

        boolean result = taskDao.swapTask(entity.getDragId(), entity.getDragPid(), entity.getDropPid());

        return result;
    }

    @Override
    public List<BoardVo> getTaskVos(int poolId) {

        List<Board> boards = taskDao.getTasks(poolId);
        List<BoardVo> collect = boards.stream().map(board -> {

            BoardVo boardVo = new BoardVo();

            // 这里硬转
            boardVo.setPId(board.getPid());
            boardVo.setTaskId(board.getTask_id());
            boardVo.setTaskTitle(board.getTask_title());
            boardVo.setPriority(board.getPriority());
            boardVo.setStartYear(board.getStart_year());
            boardVo.setStartTime(DateUtils.string2Long(board.getStart_time()));
            boardVo.setDeadLineYear(board.getDead_line_year());
            boardVo.setDeadLineTime(DateUtils.string2Long(board.getDead_line_time()));
            boardVo.setDescript(board.getDescript());
            boardVo.setOrder(board.getOrder());

            return boardVo;
        }).collect(Collectors.toList());

        return collect;
    }


    /**
     * 根据priority字符串获取order
     * @param priority
     * @return
     */
    private int getOrder(String priority) {

        int order;

        switch (priority){
            case "锚点":
                order = -1;
                break;
            case "高":
                order = 0;
                break;
            case "中":
                order = 1;
                break;
            case "低":
                order = 2;
                break;
            case "过期":
                order = 3;
                break;
            default:
                return -2;
        }

        return order;
    }
}
